package com.neuedu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.entity.MedicalInfo;
import com.neuedu.entity.PmsConsultation;
import com.neuedu.entity.PmsOrder;
import com.neuedu.mapper.PmsConsultationMapper;
import com.neuedu.service.MedicalExaminationItemService;
import com.neuedu.service.MedicalInfoService;
import com.neuedu.service.PmsConsultationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.service.PmsOrderService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 就诊记录表 服务实现类
 * </p>
 *
 * @author 王禹
 * @since 2025-08-18
 */
@Service
public class PmsConsultationServiceImpl extends ServiceImpl<PmsConsultationMapper, PmsConsultation> implements PmsConsultationService {
    @Resource
    MedicalExaminationItemService medicalExaminationItemService;
    @Resource
    MedicalInfoService medicalInfoService;
    @Resource
    PmsOrderService pmsOrderService;
    @Override
    @Transactional
    public boolean add(PmsConsultation pmsConsultation, long openId, Integer total) {
        pmsConsultation.setVisitDate(LocalDate.now());
        pmsConsultation.setVisitTime(LocalTime.now());
        String jsonStr = pmsConsultation.getAuxiliaryExamination();
        Random random = new Random(openId);
        String medicalNo = String.format("M%s%s", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")), random.nextInt(100000, 999999));
        if (StringUtils.isNoneBlank(jsonStr)) {
            List<MedicalInfo> medicalInfos = JSONObject.parseArray(jsonStr, MedicalInfo.class);
            for (MedicalInfo medicalInfo : medicalInfos) {
                medicalInfo.setMId(medicalInfo.getId());
                medicalInfo.setId(null);
                medicalInfo.setMName(medicalExaminationItemService.getNameById(medicalInfo.getMId()));
                medicalInfo.setRegisterNo(pmsConsultation.getRegisterNo());
                medicalInfo.setCreateUser(openId);
                medicalInfo.setCreateDate(LocalDate.now());
                medicalInfo.setCreateTime(LocalTime.now());
                medicalInfo.setUserId(null);
                medicalInfo.setMedicalNo(medicalNo);
            }
            medicalInfoService.saveBatch(medicalInfos);
            PmsOrder pmsOrder = new PmsOrder(
                    null,
                    total,
                    "检查费",
                    medicalNo,
                    LocalDate.now(),
                    LocalTime.now(),
                    openId,
                    (byte)1,
                    pmsConsultation.getRegisterNo()
            );
            pmsOrderService.save(pmsOrder);
        }
        return this.save(pmsConsultation);
    }
}
